package com.oauth.application.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Widget;
import com.oauth.application.client.beans.ApplicationClientBean;
import com.oauth.application.client.rpc.ApplicationRemoteServiceAsync;
import com.oauth.components.client.rpc.OAuthAsnyCallBack;

public class ApplicationPanel extends Composite {

	private static ApplicationPanelUiBinder uiBinder = GWT.create(ApplicationPanelUiBinder.class);

	private ApplicationRemoteServiceAsync mApplicationAddService;

	private ApplicationClientBean mApplicationClientBean;

	interface ApplicationPanelUiBinder extends UiBinder<Widget, ApplicationPanel> {
	}

	@UiField
	Button addButton;

	@UiField
	Image appImage;

	public ApplicationPanel() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setService(ApplicationRemoteServiceAsync pApplicationAddRemoteServiceAsync) {
		mApplicationAddService = pApplicationAddRemoteServiceAsync;
	}

	public void setApplicationBean(ApplicationClientBean pApplicationClientBean) {
		mApplicationClientBean = pApplicationClientBean;
		appImage.setUrl(mApplicationClientBean.getAppImageUrl());
	}

	@UiHandler("addButton")
	void onClick(ClickEvent e) {
		mApplicationAddService.subscribeToApplication(mApplicationClientBean.getId(),
				new OAuthAsnyCallBack<String>() {
					public void onSuccess(String result) {
						if (result.equals("success")) {
							Window.Location.assign(mApplicationClientBean.getAddUrl());
						} else {
							Window.alert("Unable to add application.Try again after some time");
						}

					}
				});

	}

}
